package com.lbs.jiepiaisi.biz;

import android.content.Intent;
import android.util.Log;

import com.lbs.jiepiaisi.bean.UserEntity;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.UploadPicturesParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.UrlUtil;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 *
 * 时间：2017/8/9 17:00
 * 作用：上传多图
 */

public class PhotoUploadBiz {
    public void uploadBiz(List<File>fileList,String eqId,String installer,
                          String installerPhone,String installerPosition,String eqPosition) {

        Log.e("PhotoActivity2", "PhotoUploadBiz:" + eqId);
        Log.e("PhotoActivity2", "PhotoUploadBiz:" + installer);
        Log.e("PhotoActivity2", "PhotoUploadBiz:" + installerPhone);
        Log.e("PhotoActivity2", "PhotoUploadBiz:" + installerPosition);
        Log.e("PhotoActivity2", "PhotoUploadBiz:" + eqPosition);


        OkHttpClient.Builder builder = null ;
        File sdcache = App.instance.getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        try {

            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
        }catch (NullPointerException e){
            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS);
        }

        OkHttpClient mOkHttpClient=builder.build();
        MultipartBody.Builder mbody = new MultipartBody.Builder().setType(MultipartBody.FORM);

        for (File file : fileList) {
            if(file != null){
                if (file.exists()) {
                    mbody.addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/png"),file));

                }
            }
        }

        mbody.addFormDataPart("eqId",eqId);
        mbody.addFormDataPart("eqPosition",eqPosition);
        mbody.addFormDataPart("installer",installer);
        mbody.addFormDataPart("installerPhone",installerPhone);
        mbody.addFormDataPart("installerPosition",installerPosition);
        RequestBody requestBody = mbody.build();
        Request request = new Request.Builder()
                .url(UrlUtil.UPLOAD_PICTURES)
                .post(requestBody)
                .build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserEntity userEntity=new UserEntity();
                userEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.UPLOAD_PICTURE);
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonString = response.body().string();//返回结果
                UserEntity userEntity= UploadPicturesParser.parser(jsonString);
                Intent intent = new Intent(Constant.UPLOAD_PICTURE);
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
        });

    }
    public void uploadBiz(List<File>fileList, String wId, final String flag) {

        //设置超时时间及缓存
        OkHttpClient.Builder builder = null ;
        File sdcache = App.instance.getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        try {

            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
        }catch (NullPointerException e){
            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS);
        }

        OkHttpClient mOkHttpClient=builder.build();
        MultipartBody.Builder mbody = new MultipartBody.Builder().setType(MultipartBody.FORM);

        for (File file : fileList) {
            if(file != null){
                if (file.exists()) {
                    mbody.addFormDataPart("img", file.getName(), RequestBody.create(MediaType.parse("image/png"),file));

                }
            }
        }

        mbody.addFormDataPart("woId",wId);
        RequestBody requestBody = mbody.build();
        Request request = new Request.Builder()
                .url(UrlUtil.WORK_UPLOAD_PICTURES)
                .post(requestBody)
                .build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserEntity userEntity=new UserEntity();
                userEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonString = response.body().string();//返回结果
                UserEntity userEntity= UploadPicturesParser.parser(jsonString);
                Intent intent = new Intent(flag);
                try {
                    JSONObject jsonObject = new JSONObject(jsonString);
                    if (jsonObject.has("status")) {
                           userEntity.setCode(jsonObject.getInt("status"));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
        });

    }

}
